<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>server</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">选择区服:</label>
                        <div class="layui-input-inline">
                            <select name="gameArea" class="layui-select" >
                                <option value="">全部</option>
                                <option v-for="(item,index) in services" :value="item.server_id">{{item.name}}</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">商品ID</label>
                        <div class="layui-input-inline">
                            <input type="text" name="gameGoods" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">订单创建时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="createDateTimeBegin" id="createTime0"
                                   placeholder="开始时间" autocomplete="off" class="layui-input" />
                            <input type="text" name="createDateTimeEnd" id="createTime1"
                                   placeholder="截止时间" autocomplete="off" class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">订单状态</label>
                        <div class="layui-input-inline">
                            <select name="status" class="layui-select">
                                <option value="">全部</option>
                                <option value="0">待支付</option>
                                <option value="1">已支付</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">


            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">

        </script>
    </div>
</div>

<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
    var services1 = JSON.parse(localStorage.getItem('services'))


    new Vue({
        el: "#app",
        data: {
            services: JSON.parse(localStorage.getItem('services')),
            gameGoodsList:[]
        },
        mounted:function () {
            var _this = this;

            layui.use(['form', 'table','laydate'], function () {
                var isLodedTable = false;
                var $ = layui.jquery,
                    form = layui.form,
                    table = layui.table
                    ,laydate=layui.laydate;



                laydate.render({
                    elem: '#createTime0',
                    type:'datetime',
                    value: getYmd(0)+ " 00:00:00"
                });
                laydate.render({
                    elem: '#createTime1',
                    type:'datetime',
                    value: getYmd(1) + " 00:00:00"
                });

                $.get('/admin/gameGoods/paginate',{limit:9999999,channeltype:0},function (res) {
                    _this.gameGoodsList = res.data;
                    form.render();
                })//默认官方渠道商品

                var loadTable = function(){
                    console.log('服务器列表',_this.services)
                    table.render({
                        where: form.val('table-search'),
                        elem: '#currentTableId',
                        width:2000,
                        url: '/admin/payOrder/orders',
                        defaultToolbar: ['filter', 'print' ],
                        toolbar: '#toolbarDemo',
                        cols: [[
                            {field: 'id', title:"排名", width: 150},
                            {title: '商品名称',width: 150, field:"goods_id",    templet: function (row) {
                                    const goods = _this.gameGoodsList.find(goods => goods.id === row.goods_id);
                                    return goods ? goods.name : '未知商品';
                                }},
                            {title: '商品ID',width: 80, field:"goods_id"},
                            {title: '服务器区域', width: 120, field: "game_area",
                                templet: function (row) {
                                    const service = _this.services.find(service => service.server_id === String(row.game_area));
                                    return service ? service.name : '未知服务器';
                                }
                            },
                            {title: '单价',width: 100, field:"fee",templet: function (row) {
                                    return row.fee ;
                                }},
                            {title: '支付成功总金额',width: 150, field:"successfulOrderstotalfee"},
                            {title: '总订单量',width: 100, field:"order_count"},
                            {title: '订单总金额',width: 150, field:"total_fee",templet: function (row) {
                                    return row.total_fee ;
                                }},
                            {title: '支付金额占比',width: 150, field:"fee_percentage"},
                            {title: '订单占比',width: 150, field:"order_percentage"},

                            {title: '支付成功订单量',width: 150, field:"successful_orders_count"},
                            {title: '未支付订单量',width: 150, field:"unsuccessful_orders_count"},


                        ]],
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 15,
                        page: true,
                        skin: 'line'
                    });
                    isLodedTable = true;
                }

                loadTable();
                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    if (!isLodedTable){
                        loadTable();
                    } else {
                        //执行搜索重载
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                            , where: form.val('table-search')
                        }, 'data');
                    }
                    return false;
                });



                table.on('toolbar(currentTableFilter)',function (obj) {
                    var selectedData = table.checkStatus(obj.config.id).data;
                    var serverIds = selectedData.map(function(data) {
                        return data.server_id;
                    }).join(',');
                    var names = selectedData.map(function(data) {
                        return data.name;
                    }).join('|');
                    var namesEncoded = encodeURIComponent(names);
                    console.log('查看标题',obj.event)
                    if(obj.event === 'add'){ //添加
                        console.log('进来没：');
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '700px'],
                            title: '添加区服',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "detail.html"
                        });
                    }

                    if(obj.event === 'editwh'){ //维护
                        layer.open({
                            type: 2, // page 层类型取消
                            area: ['780px', '700px'],
                            title: '批量维护',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "maintain.html?serverIds=" + serverIds + "&name=" + namesEncoded
                        });
                    }
                    if(obj.event === 'stopWh'){ //取消维护批量
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '700px'],
                            title: '批量取消维护',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "stopMaintain.html?serverIds=" + serverIds + "&name=" + namesEncoded
                        });
                    }

                    if(obj.event === 'addkq'){ //批量修改状态
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '700px'],
                            title: '批量开启',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "editStatus.html?serverIds=" + serverIds + "&name=" + namesEncoded
                        });
                    }



                })

                table.on('tool(currentTableFilter)', function (obj) {
                    var data = obj.data;
                    if (obj.event==='edit'){
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '700px'],
                            title: '编辑区服',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "detail.html?id="+data.server_id
                        });
                    } else if(obj.event==='delete'){
                        layer.confirm('您确定删除吗?',{title:"提示"},function(){
                            $.post('/admin/server/'+data.id,{_method:'delete'},function(res){
                                if (res.code === 0) {
                                    layer.msg('success', {icon: 6, time: 500}, function () {
                                        location.reload();//父页面刷新
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 5})
                                }
                            })
                        })
                    } else if(obj.event === 'maintain') {
                        layer.open({
                            type: 2, //page 层类型
                            area: ['780px', '700px'],
                            title: '区服维护',
                            shade: 0.6, //遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "maintain.html?id="+data.server_id
                        });
                    } else if(obj.event === 'stopMaintain'){
                        layer.confirm('您确定取消吗?',{title:"提示"},function(){
                            $.post('/admin/server/'+data.server_id+'/stopMaintain',{_method:"PUT"},function(res){
                                if (res.code === 0) {
                                    layer.msg('success', {icon: 6, time: 500}, function () {
                                        location.reload();//父页面刷新
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 5})
                                }
                            })
                        })
                    }
                });

            });
        }
    })

</script>
</body>
</html>